from pyecharts import options as opts
from pyecharts.charts import Geo,Page,Bar
from pyecharts.globals import ChartType
from pyecharts.faker import Collector, Faker
from snapshot_selenium import snapshot as driver
from pyecharts.render import make_snapshot
from selenium import webdriver
import csv

class City():
    cityName =''
    hotValue = 0

#定义加载文件的类，读取csv文件
def load_file():
    #打开csv文件
    with open("去哪儿景点.csv","rt", encoding="utf-8") as csvfile:
        reader = csv.reader(csvfile)
        cityList = []
        #根据规则读取文件中的每一行文字
        for line in reader:
            if len(line[0].split('·'))>1:
                city = City()
                city.cityName = line[0].split('·')[1]
                city.hotValue = float(line[5].split(' ')[1])
                foundRepeatCity = 0
                for originCity in cityList:
                    if originCity.cityName == city.cityName:
                        originCity.hotValue = originCity.hotValue +city.hotValue
                        foundRepeatCity = 1
                        break
                if foundRepeatCity == 0 and Geo().get_coordinate(name = city.cityName) != None and int(city.hotValue) !=0:
                    cityList.append(city)
        outputGeoData = []
        for city in cityList:
            if Geo().get_coordinate(name = city.cityName) != None and int(city.hotValue) !=0:
                outputGeoDataRecord = (city.cityName,int(city.hotValue)*5)
                outputGeoData.append(outputGeoDataRecord)
        print(outputGeoData)
    return  outputGeoData


def geo_base(data) -> Geo:
    c = (
        #初始化地理热点图
        Geo()
        .add_schema(maptype="china")
        .add("2019国庆旅游热力图",data,ChartType.HEATMAP)
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
            visualmap_opts=opts.VisualMapOpts(),
            title_opts=opts.TitleOpts(title="2019国庆旅游热力图")
        )
    )
    return c

bar = (
    Bar()
    .add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
    .add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
    .add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
    .set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况"))
)


if __name__ == '__main__':
    geo_base(load_file()).render(path="2019国庆旅游热力图.html")
    # make_snapshot(driver, geo_base(load_file()).render(), "2019国庆旅游热力图.png")
    browser = webdriver.Chrome()
    browser.get('file:///E:/python37/myFirstPython/test/2019国庆旅游热力图.html')
    # make_snapshot(driver, bar.render(), "bar.png")
    # bar.render()
